我创建了一个包含网址的列表视图,例如abc:554 / user = admin& password =& channel = 1
当我点击listview时,url会被传递到下一个类并将其用于setDataSource。但是我遇到的问题是setDataSource没有检测到url。我试图对url进行硬编码并使用Toast来在尝试连接时显示url并且它正常工作。但是当传递字符串时,没有任何反应。这是我的代码
第一堂课试图传递字符串
OnItemClickListener getURLOnItemClickListener
= new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String clickedFile = (String) parent.getItemAtPosition(position);
getURL(clickedFile);
}
};
void getURL(final String file){
if (clickAble == true){
FileInputStream fis;
String content = "";
try {
fis = openFileInput(file);
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
//Toast.makeText(getBaseContext(),content,Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent i = new Intent(addressActivity.this, liveActivity.class);
String strName = content.toString();
i.putExtra("urlAddress", strName);
startActivity(i);
}
}
设置传递字符串的第二个类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live);
final String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString = null;
} else {
newString = extras.getString("urlAddress");
}
} else {
newString = (String) savedInstanceState.getSerializable("urlAddress");
}
urlLink = "rtsp://" + newString + "&stream=1.sdp?";
videoPlay();
}
void videoPlay(){
mPlayer1 = new MediaPlayer();
mCallback1 = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mPlayer1.setDataSource(urlLink);
mPlayer1.setDisplay(surfaceHolder);
mPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mPlayer1.start();
Toast.makeText(getBaseContext(),"Connecting...",Toast.LENGTH_LONG).show();
}
});
mPlayer1.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mPlayer1.release();
}
};
final SurfaceView surfaceView1 =
(SurfaceView) findViewById(R.id.surfaceView1);
// Configure the Surface View.
surfaceView1.setKeepScreenOn(true);
// Configure the Surface Holder and register the callback.
SurfaceHolder holder1 = surfaceView1.getHolder();
holder1.addCallback(mCallback1);
holder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
答案 0 :(得分:0)
我设法找到了答案,这很荒谬简单。在第二类围绕这些代码行顶部
final String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString = null;
} else {
newString = extras.getString("urlAddress");
}
} else {
newString = (String) savedInstanceState.getSerializable("urlAddress");
}
urlLink = "rtsp://" + newString + "&stream=1.sdp?";
videoPlay();
}
我做的只是改变
urlLink = "rtsp://" + newString + "&stream=1.sdp?";
到
urlLink = "rtsp://" + newString.toString().trim() + "&stream=1.sdp?";
只是添加toString()不起作用,所以我尝试添加trim()并且它有效。如果有人可以向我解释为什么这项工作会非常感激