회원가입아이디/비번찾기
홈으로


Android Playing Audio from a web url
8년 전
http://upadhyayjiteshandroid.blogspot.kr/2013/01/android-playing-audio-from-web-url.html


 

Please make a project with the name AudioDemo and make a main class with the name  AudioDemo.java and have the following code


package com.jitesh.audiodemo;

import java.io.IOException;

import android.app.Activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

public class AudioDemo extends Activity implements

MediaPlayer.OnCompletionListener {
private static ProgressDialog progressDialog;
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private ImageButton replay;
private MediaPlayer mp;

@Override

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

play = (ImageButton) findViewById(R.id.play);

pause = (ImageButton) findViewById(R.id.pause);
stop = (ImageButton) findViewById(R.id.stop);
replay = (ImageButton) findViewById(R.id.replay);

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();
}
});
replay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setup();
}
});

setup();

}

@Override

public void onDestroy() {
super.onDestroy();

if (stop.isEnabled()) {

stop();
}
}

public void onCompletion(MediaPlayer mp) {

stop();
}

private void play() {

Log.d("play", "reached");
mp.start();

play.setEnabled(false);

pause.setEnabled(true);
stop.setEnabled(true);
}

private void stop() {

Log.d("stop", "reached");
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);

try {

setup();
} catch (Throwable t) {
goBlooey(t);
}
}

private void pause() {

Log.d("pause", "reached");
mp.pause();

play.setEnabled(true);

pause.setEnabled(false);
stop.setEnabled(true);
}

private void loadClip() {

try {
mp = MediaPlayer.create(this, R.raw.clip);
mp.setOnCompletionListener(this);
} catch (Throwable t) {
goBlooey(t);
}
}

private void setup() {

try {
progressDialog = ProgressDialog.show(AudioDemo.this, "",
"Buffering audio...", true);
progressDialog.setCancelable(true);
mp = new MediaPlayer();
mp.setDataSource("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
mp.prepareAsync();

mp.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
Log.d("first", "reached");
// mp.start();
progressDialog.dismiss();
}
});

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}

private void goBlooey(Throwable t) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Exception!").setMessage(t.toString())

.setPositiveButton("OK", null).show();
}
}

the main.xml shoupd look like this

<?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"
    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton

            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:paddingRight="4dip"
            android:src="@drawable/play" />

        <TextView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Play"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton

            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="4dip"
            android:src="@drawable/pause" />

        <TextView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Pause"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton

            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="4dip"
            android:src="@drawable/stop" />

        <TextView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Stop"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip" >

        <ImageButton

            android:id="@+id/replay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="4dip"
            android:src="@drawable/restart" />

        <TextView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:text="Replay"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</LinearLayout>


do not foget to add the permission at manidest

<uses-permission android:name="android.permission.INTERNET" />
the screen shots are attached as follows

 
 

 

 
the used resources are give below as drawables.

 

 

 

 
 
 
 
 


Yo can download the audio from a link AudioDemo
추천추천 : 371 추천 목록
번호 제목
1,348
Windows 서비스 관리자에서 Apache 서버가 멈췄을 때 시스템 자체를 재부팅
1,347
윈도우에서 Apache 서버 상태를 체크한 후 문제가 발생하면 자동으로 재부팅
1,346
윈도우 서버 2019 취약점 점검 보안
1,345
윈도우 서버 2019 취약점 점검 보안 (windows server 2019)
1,344
Windows 취약점진단 보안가이드라인
1,343
Windows Admin Center를 통한 서버 관리
1,342
윈도우 서버에서 실행되는 서비스 확인
1,341
Chrome NET::ERR CERT REVOKED 해결방법
1,340
cmd 명령어 (명령 프롬프트 명령어) 모음
1,339
Windows10 특정 프로그램(OCS 2007 R2)에서 첨부파일 드래그앤드롭이 안 되는 현상
1,338
윈도우 로그, 관리 이벤트 삭제
1,337
클린 부팅
1,336
Windows 구성 요소 저장소에서 파일 손상 검사
1,335
Windows Defender 검사 기록 삭제하기
1,334
간단한 윈도우 10 정품 인증 (크랙프로그램 필요없음)
1,333
오류난 폴더 강제삭제 방법
1,332
크롬에서 플래시 항상 허용하도록 설정하기 (레지스트리) reg 파일 만들기
1,331
GPT 디스크를 MBR 디스크로 변환
1,330
MBR 디스크를 GPT 디스크로 변환
1,329
구글 검색을 200% 활용하게 해주는 검색 명령어 총정리
1,328
[Jquery] jQuery로 우클릭 방지, 드래그 방지, 선택 방지 (IE10, 파이어폭스, 크롬 확인)
1,327
php 사용자 접속IP, 브라우저정보, os정보, http, https 접속프로토콜 알아오기
1,326
[PHP] IE 브라우저 접속 검출하기
1,325
윈도우10 시스템 예약 파티션 확인 및 삭제
1,324
윈도우10 복구 파티션 삭제 방법
1,323
윈도우10 부팅지연 검은화면에서 몇분간 머무는 현상 해결방법
1,322
삼성노트북 바이오스 진입이 불가능한 경우 바이오스 재설치와 NVRAM 초기화
1,321
익스플로러(IE)의 구글 검색공급자 한글로 변경 방법
1,320
윈도우 10 기본 앱 삭제 및 복구
1,319
meta 태그 http-equiv 설정방법과 차이점
목록
뮤직트로트 부산광역시 부산진구 가야동 ㅣ 개인정보취급방침
Copyright ⓒ musictrot All rights reserved.