简体   繁体   English

Android流实时音频

[英]Android stream live audio

I am currently making an app that plays live radio. 我目前正在制作一个可以播放直播的应用。 I made a button in my MainActivity class, which starts the play service like so: 我在MainActivity类中创建了一个按钮,如下所示启动了播放服务:

Intent intent = new Intent(MainActivity.this, StreamRadio.class);
startService(intent);

Here is the code in my StreamRadio class. 这是我的StreamRadio类中的代码。

public class StreamRadio extends Service implements MediaPlayer.OnErrorListener {
    private static final String TAG = "Information progress";
    public MediaPlayer mediaPlayer;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource("http:/xx.xx.xxx.xxx:xxxx/");
            Log.d(TAG, "Connected to http:/xx.xx.xxx.xxx:xxxx");
            mediaPlayer.prepareAsync();
            Log.d(TAG, "Preparing Async");
        } catch (IOException e) {
            Log.e("TAG", "IOException");
        }
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                Log.d(TAG, "Mediaplayer prepared");
                mediaPlayer.start();
            }
        });

        Log.d(TAG, "Nearly finished");

        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service is destroyed");

        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}

When the button in MainActivity is clicked again, the service is stopped. 当再次单击MainActivity中的按钮时,该服务将停止。

The service gets started and destroyed on click. 该服务将启动并在单击时销毁。 This is the logcat output (physical device): 这是logcat输出(物理设备):

02-03 09:42:05.719 9318-9318/ D/Information progress: Connected to http:/81.18.165.234:8361
02-03 09:42:05.719 9318-9318/ D/Information progress: Preparing Async
02-03 09:42:05.719 9318-9318/ D/Information progress: Nearly finished
02-03 09:42:05.729 9318-9318/ E/MediaPlayer: Error (1,-1)

On an emulator I get this error: 在模拟器上,我得到此错误:

02-03 10:08:26.054 2797-2797/ D/Information progress: Connected to http:/81.18.165.234:8361
02-03 10:08:26.055 2797-2797/ D/Information progress: Preparing Async
02-03 10:08:26.056 2797-2797/ D/Information progress: Nearly finished
02-03 10:08:26.060 2797-2808/ E/MediaPlayer: error (1, -2147483648)
02-03 10:08:26.060 2797-2797/ E/MediaPlayer: Error (1,-2147483648)

So I guess my question is as follows: Why is prepareAsync not working correctly, since there's no logcat message "Mediaplayer prepared", and how can I solve this? 因此,我想我的问题如下:为什么因为没有logcat消息“ Mediaplayer ready”,所以prepareAsync为什么不能正常工作,我该如何解决呢? I gave the app internet permission in the manifest. 我在清单中授予了应用Internet许可。 Other problems on SO don't seem to be the same as mine. SO上的其他问题似乎与我的不同。

Thanks in advance! 提前致谢!

I think the issue is in your datasource , check if you are assigning "Stream URL" to your datasource. 我认为问题出在您的数据源中 ,请检查是否要为数据源分配“流URL” eg http://live.streamtheworld.com:80/WXYZFMMOBILECMP3 例如http://live.streamtheworld.com:80/WXYZFMMOBILECMP3

OR 要么

Try the link below, It may help: https://github.com/tjnicolaides/Radio-Station-Stream-App-for-Android 尝试下面的链接,可能会有所帮助: https : //github.com/tjnicolaides/Radio-Station-Stream-App-for-Android

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

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