简体   繁体   English

Android在线音乐播放器

[英]Android online Music Player

i try to build Android online music player that download and play song in same time and user can heard music in download time (before complete download). 我尝试构建可同时下载和播放歌曲的Android在线音乐播放器,并且用户可以在下载时间内听到音乐(完全下载之前)。 i use below download class and MediaPlayer to start music but this class just work when download complete and if user click play before complete download application crashed with NullpointExption is there any solution...!??? 我使用下面的下载类和MediaPlayer来开始音乐,但是该类仅在下载完成后起作用,并且如果用户在完整下载应用程序之前单击播放,而NullpointExption崩溃了,有什么解决方法...!

Download Class ( Resume able) : 下载类(可恢复):

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();

}}

and in MainActivity : 在MainActivity中:

    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();
        }
    });

You need to do the prepare() before the start() 您需要在start()之前做prepare()

The android reference is pretty well written: http://developer.android.com/reference/android/media/MediaPlayer.html android参考写得很好: http : //developer.android.com/reference/android/media/MediaPlayer.html

And take a look at this tutorial, http://sapandiwakar.in/tutorial-how-to-manually-create-android-media-player-controls/ , I think it should move you along a bit as well. 并看一下本教程http://sapandiwakar.in/tutorial-how-to-manually-create-android-media-player-controls/ ,我认为它也应该使您有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM