Vitamio组件异常强大,请自行百度,android开源的媒体处理框架,无所不能
本项目需要Vitamio项目作为引用项目(Android Library)
package com.pas.radiomms;import java.io.IOException;import com.pas.radiomms.R;import com.pas.radio.helper.RadioHelper;import io.vov.vitamio.MediaPlayer;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity{ MediaPlayer mplayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this)) return; setContentView(R.layout.activity_main); } public void play(View view) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException { String path = "mms://media.crinewsradio.cn/crinewsradio"; path = "mms://211.89.225.144/cnr001"; if (RadioHelper.mplayer != null) { RadioHelper.Stop(); RadioHelper.Close(); } RadioHelper.PlaySms(path, this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.QUIT_APP) { RadioHelper.Close(); finish(); } return true; }}
播放类:
package com.pas.radio.helper;import android.content.Context;import io.vov.vitamio.MediaPlayer;public class RadioHelper{ public static MediaPlayer mplayer; public static void PlaySms(String path, Context content) { mplayer = new MediaPlayer(content); mplayer.setAdaptiveStream(true); try { mplayer.setDataSource(path); mplayer.prepare(); mplayer.start(); } catch (Exception e) { e.printStackTrace(); } } public static void Stop() { mplayer.stop(); } public static void Close() { mplayer.release(); }}