首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android在线音乐播放器

Android在线音乐播放器
EN

Stack Overflow用户
提问于 2015-01-14 02:37:26
回答 1查看 1.7K关注 0票数 1

我尝试建立Android在线音乐播放器,在下载和播放歌曲的同时,用户可以在下载时间(完成下载之前)听到音乐。我使用下面的download类和MediaPlayer来启动音乐,但是这个类只在下载完成时工作,如果用户在下载完成之前单击播放,使用NullpointExption下载应用程序崩溃,有什么解决方案...!?

Download Class (可恢复):

代码语言:javascript
复制
public class downloader {
private static boolean Stopdownloader = false;//use for stop download


public static void Stop(boolean status) {
    Stopdownloader = status;
}


public static void dl(final String downloadPath, final String filePath, final Ondownloadprogress lisener) {
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                int downloadedSize = 0;
                int fileLength = 0;
                URL url = new URL(downloadPath);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                File file = new File(filePath);
                if (file.exists()) {
                    connection.setRequestProperty("Range", "bytes=" + file.length() + "-");
                    downloadedSize = (int) file.length();
                }
                connection.setRequestMethod("GET");
                connection.setDoInput(true);
                final int FILE_SIZE = connection.getContentLength();
                connection.connect();
                fileLength = connection.getContentLength() + downloadedSize;
                BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
                RandomAccessFile output = new RandomAccessFile(file, "rw");
                output.seek(downloadedSize);

                byte data[] = new byte[1024];
                int count = 0;
                int progress = 0;

                while ((count = input.read(data, 0, 1024)) != -1
                        && progress != 100)
                {
                    if (Stopdownloader) {
                        break;
                    }
                    downloadedSize += count;
                    output.write(data, 0, count);
                    progress = (int) ((downloadedSize * 100) / fileLength);
                    final int ss = (int) ((downloadedSize * 100) / fileLength);
                    final float persent = ((float) progress / FILE_SIZE) * 100;
                    if (lisener != null) {
                        final int finalProgres = progress;
                        G.HANDLER.post(new Runnable() {

                            @Override
                            public void run() {
                                lisener.ondownload((int) ss, finalProgres, FILE_SIZE);
                            }
                        });

                    }
                }

                output.close();
                input.close();
            }
            catch (MalformedURLException e) {}
            catch (IOException e) {}
            finally {}
        }
    });

    thread.start();

}}

在MainActivity中:

代码语言:javascript
复制
    play.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            downloader.dl("http://MUSIC-URL","/temp.mp3", null);
            downloader.Stop(false);
            MediaPlayer Music = MediaPlayer.create(G.context, Uri.parse("/temp.mp3"));
            Music.start();
            Music.prepare();
        }
    });
EN

回答 1

Stack Overflow用户

发布于 2015-01-16 09:44:46

您需要在start()之前执行prepare()

android参考资料写得很好:http://developer.android.com/reference/android/media/MediaPlayer.html

看看这篇教程,http://sapandiwakar.in/tutorial-how-to-manually-create-android-media-player-controls/,我认为它也应该能让你前进一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27929135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档