简体   繁体   English

SHOUTCast Streaming Breaks in 4.0+,适用于2.3

[英]SHOUTCast Streaming Breaks in 4.0+, works in 2.3

Okay, So I'm working with a SHOUTCast stream for the first time, and I've been told that Android has changed the way it works with audio post-ICS 好的,所以我是第一次使用SHOUTCast流,有人告诉我Android已更改了其与音频后ICS一起工作的方式

So, I tried some code that worked very well on 2.3 and 2.2, but when I tried running it on 4.2.2 it didn't play, 因此,我尝试了一些在2.3和2.2上都可以很好运行的代码,但是当我尝试在4.2.2上运行时,它没有播放,

Here's my MainActivity.java 这是我的MainActivity.java

package com.mooo.ajbiz11.ponyfeather;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements
        MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
        MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {

    private String TAG = getClass().getSimpleName();
    private MediaPlayer mp = null;

    private Button play;
    private Button pause;
    private Button stop;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

        play = (Button) findViewById(R.id.play);
        pause = (Button) findViewById(R.id.pause);
        stop = (Button) findViewById(R.id.stop);

        play.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                play();
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                pause();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                stop();
            }
        });
    }

    private void play() {
        Uri myUri = Uri.parse("http://radio.ponyvillelive.com:8026/stream");
        try {
            if (mp == null) {
                this.mp = new MediaPlayer();
            } else {
                mp.stop();
                mp.reset();
            }
            mp.setDataSource(this, myUri); // Go to Initialized state
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnPreparedListener(this);
            mp.setOnBufferingUpdateListener(this);

            mp.setOnErrorListener(this);
            mp.prepareAsync();

            Log.d(TAG, "LoadClip Done");
        } catch (Throwable t) {
            Log.d(TAG, t.toString());
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "Stream is prepared");
        mp.start();
    }

    private void pause() {
        mp.pause();
    }

    private void stop() {
        mp.stop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stop();

    }

    public void onCompletion(MediaPlayer mp) {
        stop();
    }

    public boolean onError(MediaPlayer mp, int what, int extra) {
        StringBuilder sb = new StringBuilder();
        sb.append("Media Player Error: ");
        switch (what) {
            case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
                sb.append("Not Valid for Progressive Playback");
                break;
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                sb.append("Server Died");
                break;
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                sb.append("Unknown");
                break;
            default:
                sb.append(" Non standard (");
                sb.append(what);
                sb.append(")");
        }
        sb.append(" (" + what + ") ");
        sb.append(extra);
        Log.e(TAG, sb.toString());
        return true;
    }

    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
    }

}

And my activity_main.xml 还有我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <Button
                android:text="Pause"
                android:id="@+id/pause"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:layout_row="3"
                android:layout_column="9"
                android:layout_marginBottom="124dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"></Button>

         <Button
                android:text="Stop"
                android:id="@+id/stop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/AppTheme"
                android:layout_gravity="center_horizontal|bottom"
                android:layout_row="2"
                android:layout_column="15"
                android:layout_alignTop="@+id/pause"
                android:layout_toRightOf="@+id/pause"></Button>

        <Button
                android:text="Play"
                android:id="@+id/play"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|bottom"
                android:layout_row="5"
                android:layout_column="11"
                android:layout_alignTop="@+id/pause"
                android:layout_toLeftOf="@+id/pause"></Button>

    </RelativeLayout>

</LinearLayout>

This works perfectly in Android 2.3, and was made for 2.2, but I can't seem to understand the new audio stuff in 4.0+ so when I run the compiled APK (which i have in Dropbox , signed) I get nothing... 这在Android 2.3上完美运行,并且是为2.2制作的,但是我似乎无法理解4.0+中的新音频,因此,当我运行编译的APK(我在Dropbox中已签名)时,我什么也没得到...

I'm very new to Android programming and any help/insight would be amazing, but be gentle...my understanding of Java is only veeeery basic... 我对Android编程非常陌生,任何帮助/见解都会很棒,但请保持柔和...我对Java的理解只是基础知识...

Also, I would like to switch from three buttons to a MediaController, but couldn't find the documentation 另外,我想从三个按钮切换到MediaController,但是找不到文档

Pfft...figures...ran a LogCat on launch, noticed I was missing the Internet permission... Pfft ...数字...启动时运行了LogCat,注意到我缺少Internet许可...

The code is flawless, just needed the one permission and it played like butter on my tablet 该代码是完美无缺的,只需要一个权限就可以在平板电脑上像黄油一样发挥作用

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

相关问题 在2.2中流式传输shoutcast流中断的android代码 - android code for streaming shoutcast stream breaks in 2.2 WebView的4.0+ RTL库可以与2.2〜2.3的apk打包在一起吗? - Can 4.0+ RTL library for WebView be packaged with apk for 2.2~2.3? AsyncTask.doInBackground没有在2.3上调用,工作在4.0+上 - AsyncTask.doInBackground not getting called on 2.3, working on 4.0+ android:ContentResolver在4.0上有效,但在2.3上无效 - android: ContentResolver works on 4.0 but not on 2.3 Android代码可在2.3上使用,但不能在4.0上使用 - Android Code Works On 2.3 But Not On 4.0 为什么iframe可以在Android 4.0+ ICS上的webview中工作? - Why iframe works in webview on Android 4.0+ ICS? Google Cloud Messaging推送通知(带有Urban Airship)仅在Android 4.0+上显示,而不在2.3 Gingerbread上显示 - Google Cloud Messaging push notifications (with Urban Airship) only show on Android 4.0+ , not 2.3 Gingerbread data-bind =“ attr:{for:id()+&#39;check&#39;}”不适用于2.3 android,但适用于4.0+ android - data-bind=“attr:{for:id()+'check'}” do not work for 2.3 android but work for 4.0+ android 在Android 4.0+上消失的ActionBar - ActionBarDisappearing on Android 4.0+ Android主要XML在2.3和4.0上的工作方式有所不同 - android main XML works diffrent on 2.3 and 4.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM